#include "znx_hooks.h"
#include "znx_thread_base.h"

znx_hooks_t *
znx_hooks_create()
{
    znx_hooks_t *hooks;
    hooks = znx_thread_malloc(sizeof(znx_hooks_t));
    hooks->arr = znx_array_create(8, sizeof(znx_hook_handler_pt));
    return hooks;
}


void
znx_hooks_destroy(znx_hooks_t *hooks)
{
    if (hooks->arr) {
        znx_array_destroy(hooks->arr);
    }
    znx_thread_free(hooks);
}


void
znx_hooks_add(znx_hooks_t *hooks, znx_hook_handler_pt handler)
{
    znx_hook_handler_pt *hp;
    hp = znx_array_push(hooks->arr);
    *hp = handler;
}


znx_bool_t
znx_hooks_exec(znx_hooks_t *hooks, void *data)
{
    znx_hook_handler_pt *hp, h;
    hp = hooks->arr->elts;
    for (size_t i = 0; i < hooks->arr->nelts; i++) {
        h = hp[i];
        if (!h(data)) {
            return ZNX_FALSE;
        }
    }

    return ZNX_TRUE;
}

